package com.ztf0337.mycurriculumdesign.ui.manager;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ztf0337.mycurriculumdesign.R;
import com.ztf0337.mycurriculumdesign.adapter.OrderAdapter;
import com.ztf0337.mycurriculumdesign.adapter.UserAdapter;
import com.ztf0337.mycurriculumdesign.entity.Order;
import com.ztf0337.mycurriculumdesign.entity.User;
import com.ztf0337.mycurriculumdesign.service.OrderService;
import com.ztf0337.mycurriculumdesign.service.UserService;
import com.ztf0337.mycurriculumdesign.ui.activity.OrderDetailActivity;

import java.util.List;
import java.util.Objects;

public class MUserFragment extends Fragment {

    private Context context;
    private RecyclerView rvOrder;
    private UserService userService;
    private UserAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order, container, false);
        context = getContext();
        if (context == null) {
            return view;
        }
        ImageView ic_add;
        ic_add = requireActivity().findViewById(R.id.ic_add);
        ic_add.setVisibility(GONE);

        rvOrder = view.findViewById(R.id.rvOrder);
        rvOrder.setLayoutManager(new LinearLayoutManager(getContext()));
        userService = new UserService(context);

        TextView notOrder = view.findViewById(R.id.notOrder);
        List<User> userList = userService.getAll();

        if (!userList.isEmpty()) {
            notOrder.setVisibility(GONE);
            adapter = new UserAdapter(userList);
            rvOrder.setAdapter(adapter);
            adapter.setOnItemClickListener((user) -> {
                AlertDialog dialog = new AlertDialog.Builder(context)
                        .setTitle("操作该用户")
                        .setPositiveButton("删除", null)
                        .setNeutralButton(user.getStatus() ? "封禁" : "解封", null)
                        .setNegativeButton("取消", null)
                        .create();

                dialog.setOnShowListener(d -> {
                    Button deleteBtn = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
                    Button blockBtn = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);

                    deleteBtn.setOnClickListener(v -> {
                        userService.deleteUser(user);
                        adapter.removeItem(user);
                        dialog.dismiss();
                    });

                    blockBtn.setOnClickListener(v -> {
                        user.setStatus(!user.getStatus());
                        userService.updateUser(user);
                        adapter.updateItem(user);
                        dialog.dismiss();
                    });

                });


                Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawableResource(R.drawable.rounded_dialog_bg);
                dialog.show();
            });
        } else {
            notOrder.setText("无用户信息");
            notOrder.setVisibility(VISIBLE);
        }

        ImageView ic_Search = requireActivity().findViewById(R.id.ic_Search);
        EditText etSearch = requireActivity().findViewById(R.id.etSearch);
        etSearch.setText(""); // 清空内容

        etSearch.setHint("请输入电话");
        ic_Search.setOnClickListener(v -> {
            String search = etSearch.getText().toString().trim();
            if (search.isBlank()) {
                if (userList.isEmpty()) {
                    Toast.makeText(context, "无用户信息", Toast.LENGTH_SHORT).show();
                } else {
                    adapter.updateData(userList);
                }
                return;
            }
            List<User> users = userService.searchUser(search);
            if (users.isEmpty()) {
                Toast.makeText(context, "无符合条件的用户", Toast.LENGTH_SHORT).show();
            } else {
                adapter.updateData(users);
            }
        });

        return view;
    }
}
